package com.cloudsigns.factorymanager2.project.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringMainForManagerActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringMainForSuperActivity;
import com.cloudsigns.factorymanager2.engineering.activity.TeamEngineeringMainActivity;
import com.cloudsigns.factorymanager2.engineering.bean.AccountInfoBean;
import com.cloudsigns.factorymanager2.produce.activity.ProduceMainActivity;
import com.cloudsigns.factorymanager2.produce.activity.UpdatePasswordActivity;
import com.cloudsigns.factorymanager2.produce.activity.produce_team.ProduceTeamMainActivity;
import com.cloudsigns.factorymanager2.project.ProjectMainActivity;
import com.cloudsigns.factorymanager2.project.activity.PersonnelSettingListActivity;
import com.cloudsigns.factorymanager2.project.activity.PersonnelSettingListActivity1;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

/**
 * Created by tony on 2017/12/2.
 */

public class SettingFragmet extends Fragment implements View.OnClickListener {
    private RelativeLayout rl_1, rl_2, rl_3, rl_4;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_setting, null);
        rl_1 = view.findViewById(R.id.rl_1);
        rl_2 = view.findViewById(R.id.rl_2);
        rl_3 = view.findViewById(R.id.rl_3);
        rl_4 = view.findViewById(R.id.rl_4);


        rl_1.setOnClickListener(this);
        rl_2.setOnClickListener(this);
        rl_3.setOnClickListener(this);
        rl_4.setOnClickListener(this);


        AccountInfoBean accountInfoBean = MyApplicaton.getApplication().getAccountInfoBean();
        // 项目
        if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.SUPER) {

        }
        // 工程
        else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.MANAGER) {

        }
        // 班组
        else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.TEAM) {
            rl_3.setVisibility(View.GONE);
        }










        return view;
    }

    private void showExitDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("是否退出？")
                .setCancelable(true)
                .setPositiveButton("退出", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        AccountInfoBean accountInfoBean = MyApplicaton.getApplication().getAccountInfoBean();
                        if (null != accountInfoBean) {
                            Intent intent = null;

//                            if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.SUPER) {
//                                intent = new Intent(getActivity(), ProjectMainActivity.class);
////                                intent = new Intent(getActivity(), EngineeringMainForSuperActivity.class);
//                            } else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.MANAGER) {
//                                intent = new Intent(getActivity(), EngineeringMainForManagerActivity.class);
//                            } else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.TEAM) {
//                                intent = new Intent(getActivity(), TeamEngineeringMainActivity.class);
//                            }

                            // 生产
                            if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.SUPER) {
                                intent = new Intent(getActivity(), ProjectMainActivity.class);
//                                intent = new Intent(getActivity(), EngineeringMainForSuperActivity.class);
                            } else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.MANAGER) {
                                intent = new Intent(getActivity(), ProduceMainActivity.class);
                            } else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.TEAM) {
                                intent = new Intent(getActivity(), ProduceTeamMainActivity.class);
                            }

                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            intent.putExtra(ConstantUtil.KEY_1, true);
                            startActivity(intent);
//                        MyApplicaton.getApplication().clear();
//                        Intent mIntent = new Intent(getActivity(), LoginActivity.class);
//                        startActivity(mIntent);
                        }
                    }
                })
                .setNegativeButton("取消", null);
        builder.create().show();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            // 退出
            case R.id.rl_1:
                showExitDialog();

//                mIntent = new Intent(getActivity(), EngineeringMasterScheduleActivity.class);
//                startActivity(mIntent);
                break;
                // 修改密码
            case R.id.rl_2:
//                mIntent = new Intent(getActivity(), ProductionOrderListActivity.class);
                mIntent = new Intent(getActivity(), UpdatePasswordActivity.class);
                startActivity(mIntent);
                break;
                // 账户管理
            case R.id.rl_3:
               // mIntent = new Intent(getActivity(), PersonnelSettingListActivity.class);
                mIntent = new Intent(getActivity(), PersonnelSettingListActivity1.class);
                startActivity(mIntent);
                break;
            case R.id.rl_4:
//                mIntent = new Intent(getActivity(), MaintenanceOrderListActivity.class);
//                startActivity(mIntent);
                break;
        }
    }


//    private boolean mIsExit;
//    @Override
//    /**
//     * 双击返回键退出
//     */
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            if (mIsExit) {
//                this.finish();
//
//            } else {
//                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
//                mIsExit = true;
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        mIsExit = false;
//                    }
//                }, 2000);
//            }
//            return true;
//        }
//
//        return super.onKeyDown(keyCode, event);
//    }

}
